package com.lyc.controller.user;

import com.lyc.pojo.Comment;
import com.lyc.pojo.Result;
import com.lyc.service.CommentService;
import com.lyc.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/comments")
public class CommentController {
    
    @Autowired
    private CommentService commentService;
    
    /**
     * 获取帖子的评论列表（扁平结构）
     * @param postId 帖子ID
     * @return 评论列表
     */
    @GetMapping("/post/{postId}")
    public Result<List<Comment>> getPostComments(@PathVariable Integer postId) {
        List<Comment> comments = commentService.getCommentsByPostId(postId);
        return Result.success(comments);
    }
    
    /**
     * 获取帖子的分层评论列表（区分顶级评论和子评论）
     * @param postId 帖子ID
     * @return 分层评论列表
     */
    @GetMapping("/post/{postId}/hierarchical")
    public Result<List<Comment>> getHierarchicalPostComments(@PathVariable Integer postId) {
        List<Comment> comments = commentService.getHierarchicalCommentsByPostId(postId);
        return Result.success(comments);
    }
    
    /**
     * 添加新评论
     * @param comment 评论对象
     * @return 操作结果
     */
    @PostMapping("/add")
    public Result addComment(@RequestBody Comment comment) {
        try {
            boolean isSuccess = commentService.addComment(comment);
            if (isSuccess) {
                return Result.success();
            } else {
                return Result.error("添加评论失败");
            }
        } catch (IllegalArgumentException e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 删除评论
     * @param commentId 评论ID
     * @return 操作结果
     */
    @DeleteMapping("/delete/{commentId}")
    public Result deleteComment(@PathVariable Integer commentId) {
        // 获取当前登录用户ID
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        boolean isSuccess = commentService.deleteComment(commentId, userId);
        if (isSuccess) {
            return Result.success();
        } else {
            return Result.error("删除评论失败，可能评论不存在或无权限");
        }
    }
    
    /**
     * 点赞/取消点赞评论
     * @param commentId 评论ID
     * @return 操作结果
     */
    @PostMapping("/like/{commentId}")
    public Result likeComment(@PathVariable Integer commentId) {
        // 获取当前登录用户ID
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        boolean isSuccess = commentService.likeComment(commentId, userId);
        if (isSuccess) {
            return Result.success();
        } else {
            return Result.error("点赞评论失败，可能评论不存在");
        }
    }
    
    /**
     * 检查用户对某些评论的点赞状态
     * @param commentIds 评论ID，多个ID用逗号分隔
     * @param userId 用户ID
     * @return 用户点赞的评论ID集合
     */
    @GetMapping("/like/status")
    public Result<Set<Integer>> checkCommentsLikeStatus(@RequestParam String commentIds, 
                                                        @RequestParam Integer userId) {
        List<Integer> commentIdList = Arrays.stream(commentIds.split(","))
                .map(String::trim)
                .map(Integer::parseInt)
                .collect(Collectors.toList());
        
        Set<Integer> likedCommentIds = commentService.checkCommentsLikeStatus(commentIdList, userId);
        return Result.success(likedCommentIds);
    }
}